package com.symbio.ishenapi.patient.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.patient.bean.PatientBloodPressure;
import com.symbio.ishenapi.patient.bean.PatientBloodPressureResponse;
import com.symbio.ishenapi.patient.model.*;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientBloodPressure;
import io.jboot.web.controller.annotation.RequestMapping;

import java.math.BigInteger;
import java.text.Format;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author GuoShunYou
 * @Description 血压模块Controller
 * @date 下午 12:02   2018/10/23
 * @return
 */
@RequestMapping("/TmPatientBloodPressure")
public class TmPatientBloodPressureController extends BaseController {    //血压

    /**
     * @return void
     * @Author GuoShunYou
     * @Description 添加血压数据
     * @date 上午 9:52   2018/10/23
     */
    /*public void saveTmPatientBloodPressure() {

    }*/

    /**
     * @return void
     * @Author GuoShunYou
     * @Description 通过userId、runtime(日期)查询血压记录
     * @date 下午 12:03   2018/10/23
     */
    public void findTmPatientBloodPressure() {
        PatientBloodPressure tmpbp = (PatientBloodPressure) getReqBodyObject(PatientBloodPressure.class);
        if(tmpbp.getRuntime()== null || "".equals(tmpbp.getRuntime())){
            Date date=new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            tmpbp.setRuntime(formatter.format(date));
        }
        PatientBloodPressure p = TmPatientBloodPressure.dao.findTmPatientBloodPressure(tmpbp);
        renderJsonRet(p);
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通過userId查詢所有血壓記錄
     * @date 下午 5:15   2018/10/23
     * @return void
     */
    public void findAll(){
        TmPatientBloodPressure tmpbp = (TmPatientBloodPressure) getReqBodyObject(TmPatientBloodPressure.class);
        List<PatientBloodPressureResponse> parespon=TmPatientBloodPressure.dao.findAll(tmpbp);
        List<String> listruntime= TmPatientBloodPressure.dao.getRuntime(tmpbp);
        List allHeight=new ArrayList();
        List allLow=new ArrayList();

        System.out.print("返回runtime集合"+listruntime);
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<listruntime.size();i++){
            String height=TmPatientBloodPressure.dao.getHeight(listruntime.get(i),tmpbp.getUserId());
            String low=TmPatientBloodPressure.dao.getLow(listruntime.get(i),tmpbp.getUserId());
            List h=new ArrayList();
            List l=new ArrayList();
            h.add(0,listruntime.get(i));
            h.add(1,height);
            allHeight.add(h);
            l.add(0,listruntime.get(i));
            l.add(1,low);
            allLow.add(l);
            try{
                Date qian,hou;
                if(i+1<listruntime.size()){
                    qian=sdfs.parse( listruntime.get(i));
                    hou=sdfs.parse( listruntime.get(i+1));
                    System.out.println(hou+"  "+qian+"   ");
                    Long cha=(hou.getTime()-qian.getTime())/1000/60;
                    for( int j =0;j<cha/1440-1;j++){
                        Calendar c = Calendar.getInstance();
                        c.setTime(qian);
                        c.add(Calendar.DAY_OF_MONTH,1);
                        Date tom=c.getTime();
                        qian=tom;
                        List zerh=new ArrayList();

                        zerh.add(0,f.format(qian));
                        zerh.add(1,"0");

                        List zerl=new ArrayList();

                        zerl.add(0,f.format(qian));
                        zerl.add(1,"0");

                        allHeight.add(zerh);
                        allLow.add(zerl);
                    }
                }

            }catch (ParseException  e){

            }
        }
        System.out.println("高壓："+allHeight);
        System.out.println("低壓："+allLow);
        Map<String,List> maps=new HashMap<String,List>();
        maps.put("heights",allHeight);
        maps.put("lows",allLow);
        System.out.println(maps.toString());


        renderJsonRet(maps);
    }


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 添加/修改血压
     * @date 下午 2:21   2018/10/24
     * @return void
     */
    public void updTmPatientBloodPressure(){
        PatientBloodPressure patie = (PatientBloodPressure) getReqBodyObject(PatientBloodPressure.class);
        int num=TmPatientBloodPressure.dao.selectCount(patie);
        if(num==0){
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
            //ParsePosition pos = new ParsePosition(0);
            TmPatientBloodPressure tpb = new TmPatientBloodPressure();
            tpb.setUserId(patie.getUserId());
            tpb.setCreateTime(new Date());
            try {
                String time=patie.getRuntime();
                System.out.println(time);
                System.out.println(date.parse(time));
                tpb.setRuntime(date.parse(time));
            } catch (Exception e) {
                e.printStackTrace();
            }
            tpb.save();
            for (int i = 0; i < patie.getList().size(); i++) {
                TmPatientBloodPressureAttr tmpattrHeight = new TmPatientBloodPressureAttr();
                tmpattrHeight.setPressureId(tpb.getId());
                tmpattrHeight.setPressureType("200000025");
                tmpattrHeight.setPressureValue(Integer.valueOf(patie.getList().get(i).getHeight()));
                tmpattrHeight.setTimequantum(patie.getList().get(i).getTime());
                tmpattrHeight.save();

                TmPatientBloodPressureAttr tmpattrLow = new TmPatientBloodPressureAttr();
                tmpattrLow.setPressureId(tpb.getId());
                tmpattrLow.setPressureType("200000026");
                tmpattrLow.setPressureValue(Integer.valueOf(patie.getList().get(i).getLow()));
                tmpattrLow.setTimequantum(patie.getList().get(i).getTime());
                tmpattrLow.save();
            }
            renderJsonError(40001, "血压信息");
        }else{
            BigInteger id=TmPatientBloodPressure.dao.selectId(patie);
            //进行逻辑删除
            TmPatientBloodPressure tp=new TmPatientBloodPressure();
            tp.setId(id);
            tp.setStatus("0");
            tp.update();

            List<String> listss=TmPatientBloodPressure.dao.getIdList(id);
            System.out.println(listss);
            for(int i = 0;i<listss.size();i++){
                TmPatientBloodPressureAttr pa=new TmPatientBloodPressureAttr();
                pa.setId(new BigInteger(listss.get(i)));
                pa.setStatus("0");
                pa.update();
            }

            //进行添加新数据
            int count=0;
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
            TmPatientBloodPressure tpb = new TmPatientBloodPressure();
            tpb.setUserId(patie.getUserId());
            tpb.setCreateTime(new Date());
            try {
                tpb.setRuntime(date.parse(patie.getRuntime()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            boolean b= tpb.save();
            for (int i = 0; i < patie.getList().size(); i++) {
                TmPatientBloodPressureAttr tmpattrHeight = new TmPatientBloodPressureAttr();
                tmpattrHeight.setPressureId(tpb.getId());
                tmpattrHeight.setPressureType("200000025");
                tmpattrHeight.setPressureValue(Integer.valueOf(patie.getList().get(i).getHeight()));
                tmpattrHeight.setTimequantum(patie.getList().get(i).getTime());
                boolean b1=tmpattrHeight.save();
                if(b1){
                    count++;
                }
                TmPatientBloodPressureAttr tmpattrLow = new TmPatientBloodPressureAttr();
                tmpattrLow.setPressureId(tpb.getId());
                tmpattrLow.setPressureType("200000026");
                tmpattrLow.setPressureValue(Integer.valueOf(patie.getList().get(i).getLow()));
                tmpattrLow.setTimequantum(patie.getList().get(i).getTime());
                boolean bb=tmpattrLow.save();
                if(bb){
                    count++;
                }
            }
            if(count/2==patie.getList().size() && b){
                //添加成功  进行删除逻辑数据
                tp.delete();
                for(int i = 0;i<listss.size();i++){
                    TmPatientBloodPressureAttr pa=new TmPatientBloodPressureAttr();
                    pa.setId(new BigInteger(listss.get(i)));
                    pa.setStatus("0");
                    pa.delete();
                }
            }


            renderJsonError(40001, "血压信息");

        }
    }

    /**
     * wenjun
     * 查询血压一条数据
     */
    public void findOnePressure(){
        TmPatientBloodPressure user = (TmPatientBloodPressure)getReqBodyObject(TmPatientBloodPressure.class);
        List<TmPatientBloodPressure>list=TmPatientBloodPressure.dao.findOnePressure(user);
        renderJsonRet(list);
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 快速开药  添加药品订单数据
     * @date 上午 11:51   2018/10/27
     * @return void
     */
    public void adddrugorder(){
        TmDrugOrder user = (TmDrugOrder)getReqBodyObject(TmDrugOrder.class);
        String con=user.getContext();
        con=con.replace("{","<p>");
        con=con.replace("}","个<p>");
        con=con.replace("[","");
        con=con.replace("]","");
        user.setContext(con);
        boolean b=user.save();
        if(b){
            renderJsonError(40001,"药品信息");

        }else{
            renderJsonError(40002,"药品信息");
        }
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 查询所有开药记录
     * @date 下午 5:14   2018/11/23
     * @return void
     */
    public void getdurgOrder(){
        List<TmDrugOrder> list=TmDrugOrder.dao.getlist();
        renderJsonRet(list);
        return;
    }

    /**
     * 管理端用（血压）
     *
     *
     */
    public void findPressureList(){
        String userId = getPara("userId");
        if(userId == null || "".equals(userId)){
            renderJsonError(40003, "患者编号");
            return;
        }
        Map map = TmPatientBloodPressure.dao.findPatientBloodPressure(new BigInteger(userId),getQueryModel());
        renderPageJsonRet(map.get("count"),map.get("data"));

    }

}
